package fallingGame;

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * <p>
 * Title: GameSound
 * </p>
 * <p>
 * Description: CS 343 Course Project GameSound is responsible for loading and
 * playing sound files during the game (granted sound is enabled by the program)
 * </p>
 * 
 * @author Nathanial Howard, Joe Costello, Dustin Schwingle, Jared Potter
 * @email nathanial.howard@my.uwrf.edu, anthony.costello@my.uwrf.edu,
 *        dustin.schwingle@my.uwrf.edu, jared.potter@my.uwrf.edu
 * @date December 01, 2011
 * @team Group 2
 */
public class GameSound {

	private Clip musicClip;
	private Clip hitClip;
	private boolean soundEnabled;

	/**
	 * GameSound() initializes sound settings for the game
	 * 
	 * @ensure sound is disabled and sound clips are loaded
	 */
	public GameSound() {
		soundEnabled = false;
		musicClip = loadSound("src/fallingGame/gamemusic.wav");
		hitClip = loadSound("src/fallingGame/blip.wav");
	}

	/**
	 * playMusic() enables the sound and loops the game music
	 * 
	 * @ensure sound is enabled game music plays (looping)
	 */
	public void playMusic() {
		// play background music forever
		musicClip.loop(Clip.LOOP_CONTINUOUSLY);
		// sound on
		soundEnabled = true;
	}

	/**
	 * stopAll() disables sound and stops playing the game music
	 * 
	 * @ensure sound is disabled game music stops
	 */
	public void stopAll() {
		// stop background music
		musicClip.stop();
		musicClip.setFramePosition(0);
		// sound off
		soundEnabled = false;
	}

	/**
	 * playHit() plays a short sound clip if sound is enabled
	 * 
	 * @ensure clip only plays if sound is enabled resets the hitClip audio if
	 *         one is already playing
	 */
	public void playHit() {
		if (soundEnabled) {
			if (hitClip.isRunning())
				hitClip.stop(); // Stop the player if it is still running
			hitClip.setFramePosition(0); // rewind to the beginning
			hitClip.start(); // Start playing
		}
	}

	/**
	 * loadSound(String fileName) creates a new audio stream then loads an audio
	 * file into the stream
	 * 
	 * @param fileName
	 * @require the name of an audio file
	 * @ensure audio file is loaded into audio stream an audio clip is gathered
	 *         from the audio stream
	 * @throws UnsupportedAudioFileException
	 *             , IOException, LineUnavailableException
	 */
	private Clip loadSound(String fileName) {

		Clip clip = null;
		try {
			// Open an audio input stream.
			AudioInputStream audioIn = AudioSystem
					.getAudioInputStream(new File(fileName));
			// Get a sound clip resource.
			clip = AudioSystem.getClip();
			// Open audio clip and load samples from the audio input stream.
			clip.open(audioIn);
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
		return clip;
	}
}
